/**
 * @file 路由相关的工具函数
 */
import {asyncRoutesMap} from './index';

// 生成菜单导航树
export function generateTree(parentId, data) {
    const childArr = data.filter(item => {
        return item.parentId === parentId;
    });
    if (childArr.length === 0) {
        return [];
    }
    const map = [];
    childArr.forEach(item => {
        // item.component = asyncRoutesMap[item.name];
        map.push({
            ...item,
            children: generateTree(item.id, data)
        });
    });
    return map;
}

// 生成页面路由
export function createRoutes(parentId, data = []) {
    if (!parentId) {
        return [];
    }
    const parent = data.find(item => {
        return item.parentId === parentId;
    });

    let children = data.filter(item => {
        return item.parentId !== parentId;
    });

    children = children.map(item => {
        return {...item, component: asyncRoutesMap[item.name]};
    });

    return [{...parent, component: asyncRoutesMap[parent.name], children}];
}
